package com.fbty.mt.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;

import com.fbty.base.dao.MtJdbcDao;
import com.fbty.mt.service.OtherService;

public class OtherServiceImpl implements OtherService{
	private MtJdbcDao mtJdbcDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> queryCountry() {
		String sql = "SELECT DISTINCT country FROM tab_ip_info";
		List<String> list = mtJdbcDao.getJdbcTemplate().query(sql, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				String value = rs.getString("country");
				return value;
			}
			
		});
		return list;
	}

	@Override
	public List<Map<String, String>> queryRegion(String country) {
		String sql = "SELECT DISTINCT region FROM tab_ip_info where country like ?";
		
		List<Map<String, String>> list = mtJdbcDao.getJdbcTemplate().queryForList(sql, new Object[]{country});
		
		return list;
	}

	@Override
	public List<Map<String, String>> queryCity(String country,String region) {
		String sql = "SELECT DISTINCT city FROM tab_ip_info where country like ? and region like ?";
		
		List<Map<String, String>> list = mtJdbcDao.getJdbcTemplate().queryForList(sql, new Object[]{country,region});
		
		return list;
	}

	public void setMtJdbcDao(MtJdbcDao mtJdbcDao) {
		this.mtJdbcDao = mtJdbcDao;
	}

	@Override
	public List<String> queryTypes() {
		String sql = "SELECT DISTINCT type FROM tab_ip_info";
		List<String> list = mtJdbcDao.getJdbcTemplate().query(sql, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				String value = rs.getString("type");
				return value;
			}
			
		});
		return list;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<String> onlyCountry() {
		String sql = "SELECT DISTINCT country FROM tab_only_ip_info";
		List<String> list = mtJdbcDao.getJdbcTemplate().query(sql, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				String value = rs.getString("country");
				return value;
			}
			
		});
		return list;
	}
	
	@Override
	public List<Map<String, String>> onlyRegion(String country) {
		String sql = "SELECT DISTINCT region FROM tab_only_ip_info where country like ?";
		
		List<Map<String, String>> list = mtJdbcDao.getJdbcTemplate().queryForList(sql, new Object[]{country});
		
		return list;
	}
	
	@Override
	public List<Map<String, String>> onlyCity(String country,String region) {
		String sql = "SELECT DISTINCT city FROM tab_only_ip_info where country like ? and region like ?";
		
		List<Map<String, String>> list = mtJdbcDao.getJdbcTemplate().queryForList(sql, new Object[]{country,region});
		
		return list;
	}
	
	
	@Override
	public List<String> onlyTypes() {
		String sql = "SELECT DISTINCT type FROM tab_only_ip_info";
		List<String> list = mtJdbcDao.getJdbcTemplate().query(sql, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				String value = rs.getString("type");
				return value;
			}
			
		});
		return list;
	}


	
}
